<?php

/**
 * alertaCliente actions.
 *
 * @package    cronos-doctrine
 * @subpackage alertaCliente
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class alertaClienteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->alerta_clientes = Doctrine_Core::getTable('AlertaCliente')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
      
    //Captura Variable Id Usuario
    $this->idUser =  $request->getParameter('idUser');
    $this->parent =  $request->getParameter('parent');
   
    
    $this->form = new AlertaClienteForm();
    $this->form->setWidget('usuario_id', new sfWidgetFormInputHidden(array(),array('value' => $this->idUser)));
    $this->form->setWidget('agente_id', new sfWidgetFormInputHidden(array(),array('value' => $this->getUser()->getAttribute('agente_user'))));
    $this->form->setWidget('status', new sfWidgetFormInputHidden(array(),array('value' => '1'))); 
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AlertaClienteForm();
      
    
   $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($alerta_cliente = Doctrine_Core::getTable('AlertaCliente')->find(array($request->getParameter('id'))), sprintf('Object alerta_cliente does not exist (%s).', $request->getParameter('id')));
    $this->form = new AlertaClienteForm($alerta_cliente);
    
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($alerta_cliente = Doctrine_Core::getTable('AlertaCliente')->find(array($request->getParameter('id'))), sprintf('Object alerta_cliente does not exist (%s).', $request->getParameter('id')));
    $this->form = new AlertaClienteForm($alerta_cliente);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($alerta_cliente = Doctrine_Core::getTable('AlertaCliente')->find(array($request->getParameter('id'))), sprintf('Object alerta_cliente does not exist (%s).', $request->getParameter('id')));
    $alerta_cliente->delete();

    $this->redirect('alertaCliente/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
      
      
      
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    
    if ($form->isValid())
    {
      $alerta_cliente = $form->save();
      

      $this->redirect('alertaCliente/edit?id='.$alerta_cliente->getId());
    }
  }
  
   public function executeCount(sfWebRequest $request)
  {
       
        $this->horaActual =  $request->getParameter('horaActual');
        $mensajeAlerta = new AlertaCliente();
        $this->mensaje_alertas = $mensajeAlerta->countMensaje($this->getUser()->getAttribute('agente_user'),$this->horaActual);
        $this->permisos = $this->getUser()->getGuardUser()->getPermissions();
        
        
       if(count($this->mensaje_alertas)<=0){
           
           echo count($this->mensaje_alertas);
           exit;
           
       }
  }
}
